<template>
	<view class="searchbg">
		<image src="../../static/cg.png" mode=""></image>
		<view class="searchbox">
			<input type="text" placeholder="请输入金额" v-model="money" />
		</view>
		<view class="searchbox" style="top: 600rpx; height: 200rpx">
			<textarea type="text" placeholder="捐赠原因（非必填）" v-model="memo" />
		</view>
	</view>
	<view class="searchbtn" @click="submit">确认捐赠</view>
</template>

<script setup>
import { ref } from 'vue';

import { onLoad, onBackPress } from '@dcloudio/uni-app';
import { addDonate } from '@/api/ancestra.js';
import { getOpenId } from '@/api/account';
import { payment } from '@/utils/payment';

let id = ref('');

let money = ref('');
let memo = ref('');

function submit() {
	if (!uni.getStorageSync('token')) {
		uni.showModal({
			title: '您当前未登录,是否匿名捐赠',
			confirmText: '他族捐赠',
			cancelText: '族人捐赠',
			success: (e) => {
				if (e.confirm) {
					pay(wxCode.value);
				}
				if (e.cancel) {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}
			}
		});
	} else {
		pay(wxCode.value);
	}
}
async function pay(code) {
	// payment();
	if (!money.value || money.value <= 0) {
		uni.showToast({
			title: '请输入金额',
			icon: 'none'
		});
		return;
	}
	uni.setStorageSync('desc', { money: money.value, reason: memo.value });
	if (!uni.getStorageSync('openId')) {
		getWeChatCode();
	}
	let data = {
		price: money.value,
		reason: memo.value,
		payType: 0,
		type: uni.getStorageSync('token') ? 1 : 0,
		ancestralId: id.value,
		modelType: 1,
		openId: uni.getStorageSync('openId')
	};
	uni.showLoading({
		mask: true
	});

	addDonate(data).then((result) => {
		console.log(result, '====');
		let res = JSON.parse(result.data.payInfo);
		payment(res);
		uni.hideLoading();
	});
}

onLoad((e) => {
	id.value = e.id;
	isWeixin.value = isWechat();
	if (!uni.getStorageSync('openId')) {
		if (isWeixin.value) {
			checkWeChatCode(); //通过微信官方接口获取code之后，会重新刷新设置的回调地址【redirect_uri】
		}
	}
});

const isWeixin = ref(false);
let wxCode = ref('');

// 授权
/*微信登录相关  start*/
//方法：用来判断是否是微信内置的浏览器
const isWechat = () => {
	return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === 'micromessenger';
};
//方法:用来提取code
const getUrlCode = (name) => {
	return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null;
};
//检查浏览器地址栏中微信接口返回的code
const checkWeChatCode = async () => {
	let code = getUrlCode('code');
	wxCode.value = code;
	if (code) {
		const res = await getOpenId({ wxCode: code });
		if (res.code == 200) {
			uni.setStorageSync('openId', res.data.openid);
		}
	}
};
//请求微信接口，用来获取code
const getWeChatCode = () => {
	let local = encodeURIComponent(window.location.href); //获取当前页面地址作为回调地址
	let appid = 'wx8063fe9f7ea16676';

	//通过微信官方接口获取code之后，会重新刷新设置的回调地址【redirect_uri】
	window.location.href =
		'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + local + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
};
</script>
<style></style>

<style scoped lang="scss">
.searchbtn {
	position: fixed;
	bottom: 100rpx;
	left: 40rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background: #a9070c;
	color: #ffffff;

	width: 670rpx;
	height: 88rpx;
	border-radius: 50rpx 50rpx 50rpx 50rpx;
}

.searchbg {
	width: 100%;
	height: 500rpx;
	position: relative;

	& > image {
		width: 100%;
		height: 500rpx;
	}

	.searchbox {
		width: 690rpx;
		margin-left: 30rpx;
		height: 120rpx;
		position: absolute;
		bottom: -60rpx;
		background: #ffffff;
		display: flex;
		align-items: center;
		// justify-content:space-between;
		box-sizing: border-box;
		padding: 0 30rpx;
		border-radius: 20rpx;
		box-shadow: 4rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.2);
		image {
			width: 40rpx;
			height: 40rpx;
		}

		input {
		}
		textarea {
			height: 160rpx;
			// background: #A9070C;
			width: 100%;
			margin-left: 10rpx;
		}
	}
}
</style>
